package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.chat.view.MemberProfileView;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.group.TIMGroupManagerExt;
import com.tencent.imsdk.ext.group.TIMGroupMemberResult;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import java.util.ArrayList;
import java.util.List;

public class MemberProfilePresenter extends BaseFragmentPresenter<MemberProfileView> {

  public MemberProfilePresenter(Activity activity,
      MemberProfileView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void setRoleType(String groupId, String identifier, boolean isAdmin) {
    iView.showLoadingDialog();
    TIMGroupManagerExt.ModifyMemberInfoParam param =
        new TIMGroupManagerExt.ModifyMemberInfoParam(groupId, identifier);
    if (isAdmin) {
      param.setRoleType(TIMGroupMemberRoleType.Admin);
    } else {
      param.setRoleType(TIMGroupMemberRoleType.Normal);
    }

    TIMGroupManagerExt.getInstance().modifyMemberInfo(param, new TIMCallBack() {
      @Override
      public void onError(int code, String desc) {
        if (null == iView) return;
        iView.cancelLoadingDialog();
        iView.showToast("设置失败，请稍后再试～");
        iView.setRoleTypeFail();
      }

      @Override
      public void onSuccess() {
        if (null == iView) return;
        iView.cancelLoadingDialog();
      }
    });
  }

  public void deleteMember(String groupId, String identifier) {
    iView.showLoadingDialog();
    List<String> ids = new ArrayList<>();
    ids.add(identifier);
    TIMGroupManagerExt.DeleteMemberParam param =
        new TIMGroupManagerExt.DeleteMemberParam(groupId, ids);
    TIMGroupManagerExt.getInstance().deleteGroupMember(param,
        new TIMValueCallBack<List<TIMGroupMemberResult>>() {
          @Override public void onError(int i, String s) {
            if (null == iView) return;
            iView.showToast("踢出失败，请稍后再试～");
            iView.cancelLoadingDialog();
          }

          @Override public void onSuccess(List<TIMGroupMemberResult> timGroupMemberResults) {
            if (null == iView) return;
            iView.cancelLoadingDialog();
            iView.deleteMemberSuc();
          }
        });
  }
}
